/**
 * Problem: UVa 10591 Happy Number
 * Author: chchwy
 * Last Modified: 2010.01.30
 */
#include<cstdio>

char happy[1000]={
    0,1,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,1,
    0,0,0,1,0,0,0,0,1,0,0,1,1,0,0,0,0,0,0,0,
    0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,1,
    0,0,1,0,0,0,1,0,0,0,0,1,0,0,1,0,0,1,0,0,
    1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,1,1,0,0,1,0,0,0,0,0,1,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,
    0,0,0,0,0,0,0,0,1,0,1,0,1,1,0,0,0,0,0,0,
    0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,
    0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,1,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,
    0,1,1,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,
    1,0,0,0,0,0,1,0,0,1,0,1,0,0,0,0,0,0,1,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,
    0,0,1,0,0,1,0,1,1,0,0,0,0,0,0,0,1,0,0,1,
    0,0,0,1,0,0,1,0,0,0,0,1,1,0,0,0,0,1,0,0,
    0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,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0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0
};

//取n的各位數平方和
int toNext(int n) {
    int sum=0;
    while ( n!=0 ) {
        int tmp = n%10;
        sum += tmp*tmp;
        n /= 10;
    }
    return sum;
}

bool isHappy(int n) {
    while (n>1000)
        n = toNext(n);
    return happy[n];
}

int main() {
	#ifndef ONLINE_JUDGE
    freopen("10591.in","r",stdin);
	#endif

    int numCase;
    scanf("%d", &numCase );

    for (int i=1;i<=numCase;++i) {
        int n;
        scanf("%d",&n );
        if ( isHappy(n) )
            printf("Case #%d: %d is a Happy number.\n",i,n);
        else
            printf("Case #%d: %d is an Unhappy number.\n",i,n);
    }
    return 0;
}
